from zhipuai import ZhipuAI
import base64
import time

def image_change():
    with open("D:\\1.png", 'rb') as image_file:
        my_str = base64.b64encode(image_file.read()).decode('utf-8')
    return my_str
base64_str = image_change()
# print(base64_str)

        
          

client = ZhipuAI(api_key="69abb0f1108266a676a117ce132968e8.142O8E9qeuCYldsa") # 填写您自己的APIKey
response = client.chat.completions.create(
    model="glm-4v",  # 填写需要调用的模型名称
    messages=[
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "图里有什么"
          },
          {
            "type": "image_url",
            "image_url": {
                "url" : base64_str 
            }
          }
        ]
      }
    ]
)
print(response.choices[0].message.content)
with open('messages_with_timestamps.txt', 'a', encoding='utf-8') as file:
    
        # 获取当前时间戳
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        
        # 构造带有时间戳的消息内容
        message_with_timestamp = f"{timestamp}: {response.choices[0].message.content}\n"
        
        # 写入文件
        file.write(message_with_timestamp)

